<template>
  <div>
    <Pane>
      <span slot="title">账号列表</span>
      <section slot="content">
        <el-table
          ref="multipleTable"
          :data="accountData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="account" label="账号"></el-table-column>
          <el-table-column prop="userGroup" label="用户组"></el-table-column>
          <el-table-column label="创建时间">
            <template slot-scope="scope">{{ scope.row.ctime }}</template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="accountEdit(scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="accountDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
          <el-button type="danger" @click="accountListDel">批量删除</el-button>
          <el-button type="primary" @click="cancelDel">取消选择</el-button>
        </div>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2, 4, 6]"
          :page-size="2"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
        <!-- 弹出框 -->
        <el-dialog title="账号编辑" :visible.sync="dialogFormVisible">
          <el-form :model="accountForm">
            <el-row>
              <el-col :span="12">
                <el-form-item label="账号:">
                  <el-input v-model="accountForm.account" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="用户组:">
                  <el-select v-model="accountForm.userGroup" placeholder="请选择">
                    <el-option label="普通管理员" value="普通管理员"></el-option>
                    <el-option label="超级管理员" value="超级管理员"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="goodsChangeSave">确 定</el-button>
          </div>
        </el-dialog>
      </section>
    </Pane>
  </div>
</template>

<script>
import Pane from '@/components/Pane/Pane.vue'
import { getAccountList, delAccount, updateAccount, delAccountList } from '@/api/account.js'
import moment from 'moment'

export default {
  name: 'AcountList',
  props: {},
  data() {
    return {
      accountData: [],
      total: 0,
      currentPage: 1,
      pageSize: 2,
      dialogFormVisible: false,
      // 弹出框内容
      accountForm: {},
      // 被选中的项---删除项
      ids: []
    }
  },
  methods: {
    async init() {
      const { total, data } = await getAccountList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      data.forEach(v => {
        v.ctime = moment(v.orderTime).format('YYYY-MM-DD HH:MM:SS')
      })
      this.total = total
      this.accountData = data
    },
    // 分页功能函数
    handleSizeChange(val) {
      this.pageSize = val
      this.init()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.init()
    },
    // 单个删除
    async accountDel(id) {
      // console.log(id)
      const { code } = await delAccount({ id: id })
      if (code === 0) {
        this.init()
      }
    },
    // 编辑
    accountEdit(row) {
      this.accountForm = row
      this.select = row.userGroup
      this.dialogFormVisible = true
      // console.log(this.accountForm)
    },
    // 编辑后保存
    async goodsChangeSave() {
      // console.log(this.accountForm)
      this.dialogFormVisible = false
      const { code } = await updateAccount({
        id: this.accountForm.id,
        account: this.accountForm.account,
        userGroup: this.accountForm.userGroup
      })
      if (code === 0) {
        this.init()
      }
      // console.log(code)
    },
    // 批量删除
    async accountListDel() {
      if (!this.ids) {
        this.$message.error('未选择项')
        return 0
      }
      const { code } = await delAccountList({
        ids: JSON.stringify(this.ids)
      })
      // console.log(code)
      if (code === 0) {
        this.init()
      }
    },
    cancelDel() {
      this.$refs.multipleTable.clearSelection()
    },
    // 选中项发送变化
    handleSelectionChange(val) {
      // console.log(val)
      this.ids = val.map(v => v.id)
      console.log(this.ids)
    }
  },
  components: {
    Pane
  },
  created() {
    this.init()
  }
}
</script>

<style scoped >
</style>
